package com.antler.smt.portal.controller;

import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.request.admin.GetPurchaserListReq;
import com.antler.smt.request.portal.SendReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.IdentifyingCodeService;
import com.antler.smt.service.portal.PurchaserService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 采购商认证
 *
 * @author zhangyi
 * @date 2018/4/2 10:22
 */
@RestController
@RequestMapping("/purchaserCertification")
@Api(description = "采购商认证")
public class PurchaserCertificationController extends BaseController{

    /**
     * 日志
     */
    public static Logger log = LogUtil.get();

    @Resource
    private IdentifyingCodeService identifyingCodeService;

    @Resource
    private PurchaserService purchaserService;

    /**
     *  获取客户认证实体
     *
     * @param request
     * @param code
     * @return
     */
    @PostMapping("/certification/{code}")
    @ApiOperation(value = "获取客户认证实体", httpMethod = "POST")
    @OperateCut(operationContent = "采购商认证")
    public Result<ResultCodeEnum> certification(@RequestBody GetPurchaserListReq request,@PathVariable String code){

        DataValidator.isNull(request,"采购商实体不能为空！");
        DataValidator.isBlank(request.getCompanyName(),"公司名称必传！");
        DataValidator.isBlank(request.getCreditCode(),"统一信用社代码必传！");
        DataValidator.isBlank(request.getDetailAddress(),"公司地址必传");
        DataValidator.isBlank(request.getLegalPerson(),"法人必传！");
        DataValidator.isBlank(String.valueOf(request.getFoundTime()),"成立时间必传！");
        DataValidator.isBlank(request.getLinkMan(),"联系人姓名必传！");
        DataValidator.isBlank(request.getLinkEmail(),"电子邮箱必填！");
        DataValidator.isBlank(request.getLinkPhone(),"联系电话！");

        DataValidator.isNull(code,"验证码必填！");

        Result result = null;
           result = purchaserService.purchaserCertification(request,currentMember.getId(),code);
        return result;
    }

    /**
     *  验证码发送
     *
     * @param receiver 接收者邮箱
     * @return
     */
    @PostMapping("/identifyingCode")
    @ApiOperation(value = "验证码发送", httpMethod = "POST")
    @OperateCut(operationContent = "发送验证码")
    public Result identifyingCode(@RequestBody SendReq receiver){

        DataValidator.isNull(receiver,"接收实体不能为空！");

        Result result = null;
            result = identifyingCodeService.sendCode(String.valueOf(currentMember.getId()),receiver);
        return result;
    }

    /**
     *  返回认证详情信息
     *
     * @return
     */
    @GetMapping("/certificationInfo")
    @ApiOperation(value = "返回认证详情信息", httpMethod = "GET")
    @OperateCut(operationContent = "查看认证信息")
    public Result certificationInfo(){

        Result result = null;
            result = purchaserService.getCertificationInfo(currentMember.getId());
        return result;
    }
}
